package com.example.onlineeducation.ui.main

import android.annotation.SuppressLint
import android.os.Bundle
import android.view.View
import com.example.onlineeducation.App
import com.example.onlineeducation.R
import com.example.onlineeducation.base.BaseFragment
import com.example.onlineeducation.data.model.OfflineExamListDataModel
import com.example.onlineeducation.databinding.CommonListActivityBinding
import com.example.onlineeducation.databinding.OfflineExamListItemBinding
import com.example.onlineeducation.ui.offline.OfflineExamRecordListActivity
import com.example.onlineeducation.viewmodel.MainOfflineExamVM
import com.gyf.immersionbar.BarHide
import com.gyf.immersionbar.ImmersionBar
import com.yc.jetpacklib.extension.*
import com.yc.jetpacklib.recycleView.YcPagingDataAdapter
import com.yc.jetpacklib.refresh.YcRefreshBaseUtil
import com.yc.jetpacklib.refresh.YcRefreshSpecialPagingUtil


@SuppressLint("SetTextI18n")
class MainOfflineExamFragment : BaseFragment<CommonListActivityBinding>(CommonListActivityBinding::inflate) {
    private val mViewModel: MainOfflineExamVM by ycViewModels()
    private var mProId: String? = null
    private lateinit var mRefreshUtil: YcRefreshBaseUtil<OfflineExamListDataModel>

    override fun initImmersionBar() {
        ImmersionBar.with(this)
            .statusBarColor(R.color.white)
            .statusBarDarkFont(true)
            .hideBar(BarHide.FLAG_SHOW_BAR)
            .fullScreen(false)
            .fitsSystemWindows(true)
            .init()
    }

    private val mOfflineExamAdapter: YcPagingDataAdapter<OfflineExamListDataModel, OfflineExamListItemBinding> by YcPagingDataAdapter.ycLazyInitApply(
        OfflineExamListItemBinding::inflate, OfflineExamListDataModel.diffCallback
    ) {
        mOnUpdate = {
            ivExam.ycLoadImageNet(it.examImage, errorImg = R.drawable.ic_topic)
            tvExamName.text = it.examName
            tvTopic.text = it.topicName
            tvExamTime.text = it.startTime.ycToNoEmpty() + "~" + it.endTime.ycToNoEmpty()
        }
        mItemClick = { entity ->
            if (entity.id.ycIsNotEmpty())
                OfflineExamRecordListActivity.newInstance(requireActivity(), entity.id!!, entity.examName, entity.startTime, entity.endTime, entity.totalScore)
            else
                showToast("考试Id无效,请联系管理员")
        }
    }

    override fun CommonListActivityBinding.initView(view: View, savedInstanceState: Bundle?) {
        actionbarLayout.setTitle(titleName = "线下考试")
        rvList.apply {
            adapter = mOfflineExamAdapter
            ycInitLinearLayoutManage()
        }
        mRefreshUtil = YcRefreshSpecialPagingUtil<OfflineExamListDataModel>(requireActivity())
            .build(mOfflineExamAdapter, srlList, rvList, flRefresh) {
                mRefreshCall = {
                    getData()
                }
            }
        mRefreshUtil.startRefresh()

        mViewModel.mOfflineExamList.observe {
            mRefreshUtil.acSetPagingData(it)
        }

        App.mInstant.mViewModel.mProInfoEntity.observe {
            if (it != null && mProId != it.proId) {
                mProId = it.proId
                getData()
            }
        }
    }

    private fun getData() {
        mViewModel.getOfflineExamList(mProId!!)
    }

}
